package leetcode100;

import java.util.Arrays;
import java.util.LinkedList;

public class lc006 {
    public static void main(String[] args) {
        int[] temperatures = {55,38,53,81,61,93,97,32,43,78};
        int[] res = dailyTemperatures(temperatures);
        System.out.println(Arrays.toString(res));

    }

    public static int[] dailyTemperatures(int[] temperatures) {
        int n = temperatures.length;
        int[] res = new int[n];
        LinkedList<Integer> stack = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
                int indexPre =  stack.pop();
                res[indexPre] = i-indexPre;
            }
            stack.push(i);
        }

        return res;
    }
}
